package com.hkd.common.data.config.type_handler;

import com.hkd.common.api.enums.AreaType;
import com.hkd.common.api.enums.BalanceSource;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author HKD
 * @date 2022/9/17 20:36
 */
@Component
@MappedTypes(BalanceSource.class)
@MappedJdbcTypes(value = JdbcType.TINYINT, includeNullJdbcType = true)
public class BalanceSourceHandler extends BaseTypeHandler<BalanceSource> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, BalanceSource balanceSource, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i,balanceSource.getCode());
    }

    @Override
    public BalanceSource getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return BalanceSource.search(resultSet.getInt(s));
    }

    @Override
    public BalanceSource getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return BalanceSource.search(resultSet.getInt(i));
    }

    @Override
    public BalanceSource getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return BalanceSource.search(callableStatement.getInt(i));
    }
}
